package com.tjec.project.system.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import com.tjec.common.enums.CodeNum;
import com.tjec.common.utils.DateUtils;
import com.tjec.project.system.domain.BmsCategoryAssociated;
import com.tjec.project.system.domain.BmsMdBusinessCategory;
import com.tjec.project.system.domain.BmsMdTaskTemplate;
import com.tjec.project.system.mapper.BmsCategoryAssociatedMapper;
import com.tjec.project.system.mapper.BmsMdBusinessCategoryMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.tjec.project.system.mapper.BmsMdAuditWorkflowConfigMapper;
import com.tjec.project.system.domain.BmsMdAuditWorkflowConfig;
import com.tjec.project.system.service.IBmsMdAuditWorkflowConfigService;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

/**
 * 成果校审流程配置Service业务层处理
 * 
 * @author tjec
 * @date 2020-04-16
 */
@Service
public class BmsMdAuditWorkflowConfigServiceImpl implements IBmsMdAuditWorkflowConfigService 
{
    @Autowired
    private BmsMdAuditWorkflowConfigMapper bmsMdAuditWorkflowConfigMapper;
    @Autowired
    private BmsCategoryAssociatedMapper bmsCategoryAssociatedMapper;
    @Autowired
    private BmsMdBusinessCategoryMapper bmsMdBusinessCategoryMapper;

    /**
     * 查询成果校审流程配置
     * 
     * @param guid 成果校审流程配置ID
     * @return 成果校审流程配置
     */
    @Override
    public BmsMdAuditWorkflowConfig selectBmsMdAuditWorkflowConfigById(String guid)
    {
        BmsMdAuditWorkflowConfig entity = bmsMdAuditWorkflowConfigMapper.selectBmsMdAuditWorkflowConfigById(guid);
        //业务类型
        List<BmsMdBusinessCategory> bmsMdBusinessCategoryList = bmsMdBusinessCategoryMapper.selectBusinessCategoryByCategoryAssociated(guid, null);
        entity.setBusinessCategoryList(bmsMdBusinessCategoryList);
        //转化guid成List<String>
        List<String> lists= new ArrayList<String>();
        for (BmsMdBusinessCategory bmsMdBusinessCategory:bmsMdBusinessCategoryList) {
            lists.add(bmsMdBusinessCategory.getGuid()) ;
        }
        entity.setBusinessCategoryIds(lists);
        return entity;
    }

    /**
     * 查询成果校审流程配置列表
     * 
     * @param bmsMdAuditWorkflowConfig 成果校审流程配置
     * @return 成果校审流程配置
     */
    @Override
    public List<BmsMdAuditWorkflowConfig> selectBmsMdAuditWorkflowConfigList(BmsMdAuditWorkflowConfig bmsMdAuditWorkflowConfig)
    {
        List<BmsMdAuditWorkflowConfig> list = bmsMdAuditWorkflowConfigMapper.selectBmsMdAuditWorkflowConfigList(bmsMdAuditWorkflowConfig);
        list.forEach(item -> {
            item.setBusinessCategoryList(bmsMdBusinessCategoryMapper.selectBusinessCategoryByCategoryAssociated(item.getGuid(),bmsMdAuditWorkflowConfig.getBusinessCategoryId()));
        });
        return list;
    }

    /**
     * 新增成果校审流程配置
     * 
     * @param bmsMdAuditWorkflowConfig 成果校审流程配置
     * @return 结果
     */
    @Override
    @Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public int insertBmsMdAuditWorkflowConfig(BmsMdAuditWorkflowConfig bmsMdAuditWorkflowConfig)
    {
        bmsMdAuditWorkflowConfig.setCreateTime(DateUtils.getNowDate());
        int ret = bmsMdAuditWorkflowConfigMapper.insertBmsMdAuditWorkflowConfig(bmsMdAuditWorkflowConfig);
        if (ret > -1) {
            updateBusinessCategory(bmsMdAuditWorkflowConfig);
        }


        return ret;
    }
    /**
     * 更新业务类别表
     * @param entity
     */
    private void updateBusinessCategory(BmsMdAuditWorkflowConfig entity) {
        if(!CollectionUtils.isEmpty(entity.getBusinessCategoryIds())){
            List<String> categoryList = entity.getBusinessCategoryIds();
            //添加关联库的数据  业务类别的关联
            for (String bms:categoryList) {
                BmsCategoryAssociated associated = new BmsCategoryAssociated();
                associated.setCategoryGuid(bms);
                associated.setParentGuid(entity.getGuid());
                associated.setGuid(UUID.randomUUID().toString());
                associated.setFlag(CodeNum.SEVEN.getCode());
                bmsCategoryAssociatedMapper.insertBmsCategoryAssociated(associated);
            }
        }
    }

    /**
     * 修改成果校审流程配置
     * 
     * @param bmsMdAuditWorkflowConfig 成果校审流程配置
     * @return 结果
     */
    @Override
    @Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public int updateBmsMdAuditWorkflowConfig(BmsMdAuditWorkflowConfig bmsMdAuditWorkflowConfig)
    {
        bmsMdAuditWorkflowConfig.setUpdateTime(DateUtils.getNowDate());
        int ret =  bmsMdAuditWorkflowConfigMapper.updateBmsMdAuditWorkflowConfig(bmsMdAuditWorkflowConfig);
        if(ret > -1){
            //对业务类别进行修改先删除在重现添加
            bmsCategoryAssociatedMapper.deleteBmsCategoryAssociatedByParentId(bmsMdAuditWorkflowConfig.getGuid());
            updateBusinessCategory(bmsMdAuditWorkflowConfig);
        }
        return ret;
    }

    /**
     * 批量删除成果校审流程配置
     * 
     * @param guids 需要删除的成果校审流程配置ID
     * @return 结果
     */
    @Override
    public int deleteBmsMdAuditWorkflowConfigByIds(String[] guids)
    {
        return bmsMdAuditWorkflowConfigMapper.deleteBmsMdAuditWorkflowConfigByIds(guids);
    }

    /**
     * 删除成果校审流程配置信息
     * 
     * @param guid 成果校审流程配置ID
     * @return 结果
     */
    @Override
    public int deleteBmsMdAuditWorkflowConfigById(String guid)
    {
        return bmsMdAuditWorkflowConfigMapper.deleteBmsMdAuditWorkflowConfigById(guid);
    }
}
